package com.trkj.wiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.trkj.wiki.exception.BusinessException;
import com.trkj.wiki.exception.BusinessExceptionCode;
import com.trkj.wiki.mapper.UserMapper;
import com.trkj.wiki.pojo.User;
import com.trkj.wiki.pojo.UserExample;
import com.trkj.wiki.req.UserLoginReq;
import com.trkj.wiki.req.UserQueryReq;
import com.trkj.wiki.req.UserResetPasswordReq;
import com.trkj.wiki.req.UserSaveReq;
import com.trkj.wiki.resp.UserLoginResp;
import com.trkj.wiki.resp.UserQueryResp;
import com.trkj.wiki.resp.PageResp;
import com.trkj.wiki.util.CopyUtil;
import com.trkj.wiki.util.SnowFlake;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Log4j2
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Resource
    private SnowFlake snowFlake;

    public PageResp<UserQueryResp> list(UserQueryReq req){
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        if (!ObjectUtils.isEmpty(req.getLoginName())) {
            criteria.andLoginNameEqualTo(req.getLoginName());
        }
        PageHelper.startPage(req.getPage(),req.getSize());
        List<User> userList = userMapper.selectByExample(userExample);
        PageInfo<User> pageInfo=new PageInfo<>(userList);
        log.info("总行数：{}",pageInfo.getTotal());
        log.info("总页数：{}",pageInfo.getPages());

    /*    List<UserResp> respList=new ArrayList<>();
        for (User user : userList) {
            UserResp userResp = new UserResp();
            BeanUtils.copyProperties(user,userResp);
            respList.add(userResp);
        }*/

        List<UserQueryResp> respList = CopyUtil.copyList(userList, UserQueryResp.class);
        PageResp<UserQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(respList);


        return pageResp;
    }
    
    /**
     * @Author Twx
     * @Date 2021/4/25 14:34
     * @Description 电子书新增/修改
     * @Param [req]
     * @Return void
     * @Since version-1.0
     */
    public void save(UserSaveReq req){
        User user=CopyUtil.copy(req,User.class);
        if(ObjectUtils.isEmpty(req.getId())){
            User userDB = selectByLoginName(req.getLoginName());
            if(ObjectUtils.isEmpty(userDB)){
                //新增
                user.setId(snowFlake.nextId());
                userMapper.insert(user);
            }else {
                //用户名已存在
                throw new BusinessException(BusinessExceptionCode.USER_LOGIN_NAME_EXIST);
            }

        }else {
            //更新
            user.setLoginName(null);
            userMapper.updateByPrimaryKeySelective(user);
        }

    }

    /*
     * @Author Twx
     * @Date 2021/4/25 20:28
     * @Description 删除电子书
     * @Param [id]
     * @Return void
     * @Since version-1.0
     */
    public void delete(long id){
        userMapper.deleteByPrimaryKey(id);
    }


    /**
     * @Author Twx
     * @Date 2021/5/6 10:46
     * @Description 用户名重复
     * @Param [loginName]
     * @Return com.trkj.wiki.pojo.User
     * @Since version-1.0
     */
    public User selectByLoginName(String loginName){
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andLoginNameEqualTo(loginName);
        List<User> userList = userMapper.selectByExample(userExample);
        if(CollectionUtils.isEmpty(userList)){
            return null;
        }else{
            return userList.get(0);
        }
    }


    /**
     * @Author Twx
     * @Date 2021/5/7 09:26
     * @Description 重置密码
     * @Param [req]
     * @Return void
     * @Since version-1.0
     */
    public void resetPasword(UserResetPasswordReq req){
        User user=CopyUtil.copy(req,User.class);

        userMapper.updateByPrimaryKeySelective(user);

    }

    /**
     * @Author Twx
     * @Date 2021/5/7 10:48
     * @Description 登录
     * @Param [req]
     * @Return void
     * @Since version-1.0
     */
    public UserLoginResp login(UserLoginReq req){
        User userDB = selectByLoginName(req.getLoginName());
        if(ObjectUtils.isEmpty(userDB)){
            //用户名不存在
            log.info("用户名不存在，{}",req.getLoginName());
            throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
        }else{
            if(userDB.getPassword().equals(req.getPassword())){
                //登录成功
                UserLoginResp userLoginResp=CopyUtil.copy(userDB,UserLoginResp.class);
                return userLoginResp;
            }else {
                //密码不对
                log.info("密码不对，输入密码：{}，数据库密码：{}",req.getPassword(),userDB.getPassword());
                throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
            }
        }
    }


}
